package com.hxkcloud.patient.aerotel.provider;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.hxkcloud.patient.R;
import com.hxkcloud.patient.aerotel.adapter.DeviceListAdapter;
import com.hxkcloud.patient.aerotel.domain.health.HealthDevice;
import com.hxkcloud.patient.aerotel.domain.health.HealthDeviceManager;

import java.util.List;

/**
 * Created by Administrator on 2016/4/30.
 */
public class PairedDevicesActionProvider extends ActionProvider {

    private Context mContext;
    private View imgbtn = null;
    private PopupWindow popupWindow = null;

    public PairedDevicesActionProvider(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public View onCreateActionView() {
        View view = LayoutInflater.from(this.mContext).inflate(R.layout.paired_devices_menu, null, false);
        imgbtn = (View) view.findViewById(R.id.pdmenuitem);
        imgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow == null) {
                    final List<HealthDevice> devices = HealthDeviceManager.getDevices();
                    DeviceListAdapter adapter = new DeviceListAdapter(mContext, devices);
                    ListView lv = new ListView(mContext, null, R.style.DevicesListViewStyle);
                    lv.setPadding(50, 50, 50, 50);
                    TextView header = new TextView(mContext, null, R.style.DevicesListItemStyle);
                    header.setText("已配对设备");
                    header.setTextColor(Color.parseColor("#25c0a0"));
                    lv.addHeaderView(header);
                    lv.setAdapter(adapter);
                    popupWindow = new PopupWindow(lv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                    popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
                    popupWindow.setOutsideTouchable(true);
                    popupWindow.showAsDropDown(v);
                } else {
                    if (popupWindow.isShowing()) {
                        popupWindow.dismiss();
                    } else {
                        final List<HealthDevice> devices = HealthDeviceManager.getDevices();
                        DeviceListAdapter adapter = new DeviceListAdapter(mContext, devices);
                        ListView lv = new ListView(mContext, null, R.style.DevicesListViewStyle);
                        lv.setPadding(50, 50, 50, 50);
                        TextView header = new TextView(mContext, null, R.style.DevicesListItemStyle);
                        header.setText("已配对设备");
                        header.setTextColor(Color.parseColor("#25c0a0"));
                        lv.addHeaderView(header);
                        lv.setAdapter(adapter);
                        popupWindow.setContentView(lv);
                        popupWindow.showAsDropDown(v);
                    }
                }
            }
        });
        return view;
    }
}
